package cn.kust.clock.controller;

import cn.kust.clock.pojo.dto.CollectionDTO;
import cn.kust.clock.pojo.vo.CollectionVO;
import cn.kust.clock.service.CollectionService;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YL
 * @data 2022/7/26 14:46
 **/
@Slf4j
@CrossOrigin //解决跨域
@RestController
@RequestMapping("/api-clock/collection")
public class CollectionController {

    @Autowired
    CollectionService collectionService;


    /**
     * 删除收藏
     * @param collectionDTO 包含type type_id user_id status
     * @return 是否删除成功
     */
    @PostMapping("/update")
    public ResponseResult updateByCollectDTO(CollectionDTO collectionDTO) {
        log.info("进入删除收藏的Controller层");
        if (collectionDTO.getType() == null||collectionDTO.getTypeId() == null||collectionDTO.getUserId() == null) {
            return ResponseResult.fail(10086,"传入数据错误");
        }
        if (collectionDTO.getType() != 1 && collectionDTO.getType() != 2) {
            return ResponseResult.fail(10086,"type类型错误");
        }
        if (collectionDTO.getStatus() != 0 && collectionDTO.getStatus() != 1) {
            return ResponseResult.fail(10086,"status类型错误");
        }
        Integer count = collectionService.updateByCollectDTO(collectionDTO);
        if (count == 1) {
            return ResponseResult.success();
        }else {
            return ResponseResult.fail(10086,"无可删除数据");
        }
    }


    /**
     * 插入收藏记录
     * @param collectionDTO 包含type type_id user_id
     * @return 是否插入成功
     */
    @PostMapping("/insert")
    public ResponseResult insert(CollectionDTO collectionDTO) {
        log.info("进入添加收藏的Controller层");
        if (collectionDTO.getType() == null||collectionDTO.getTypeId() == null||collectionDTO.getUserId() == null) {
            return ResponseResult.fail(10086,"传入数据错误");
        }
        if (collectionDTO.getType() != 1 && collectionDTO.getType() != 2) {
            return ResponseResult.fail(10086,"type类型错误");
        }
        Integer count = collectionService.insert(collectionDTO);
        if (count == 1) {
            return ResponseResult.success();
        }else {
            return ResponseResult.fail(10086,"插入失败");
        }
    }

    /**
     * 查询用户的收藏列表
     * @param type 0--全部收藏列表 1--打卡收藏列表 2--游记收藏列表
     * @param userId 用户id
     * @return 收藏列表 包括id type typeId title
     */
    @GetMapping("/{type}/{userId}")
    public ResponseResult selectByUserId(@PathVariable("type") Integer type,@PathVariable("userId") Long userId) {
        log.info("进入总收藏的Service层");
        List<CollectionVO> collection = null;
        if (type == 0) {
            collection = collectionService.selectByUserId(userId);
        }else if (type == 1){
            collection = collectionService.selectClockByUserIdAndType(userId);
        }else if (type == 2) {
            collection = collectionService.selectTravelsByUserIdAndType(userId);
        }else {
            return ResponseResult.fail(10086,"type错误");
        }

        log.info("collection:{}",collection);
        return ResponseResult.success(collection);
    }



}
